<?php

if (!defined('IN_ADMIN')) {
    exit('Access Denied');
}

$mpurl = 'admin.php?ac=manage';
$list = array();
if($_GET ['op'] == "actionManage"){
    $uid    =    $_GET['uid'];
    $action =    $_GET['action'];
    $time   =    86400;
    if ($_GET['time']){
        $time = intval(ceil($_GET['time'])) * 86400;
    }
    $param = array(
            'do' => $action,
            'format' => 'json',
            'uid' => $uid,
            'time' => $time,
    );

    $content = get_api_content($_SC ['apiurl'], $param);

    $content = json_decode($content);
    if ($content->resultData == 1) {
        $msg = "操作成功!";
        cpmessage($msg);
    }
    $msg = "操作失败，请重试一次!";
    if ($content->errorNo) {
        $msg .= "errorNo:" . $content->errorNo;
    }
    cpmessage($msg);
	exit;
}elseif ($_GET ['op'] == "setShutUpTime"){
    $start              = intval($_GET ['start']) > 1 ? intval($_GET ['start']) : 0;
    $uid 	            = intval($_GET ['uid']);
    $length             = intval($_GET ['length']) > 1 ? intval($_GET ['length']) : 20;
    $param  = array(
            'do' 		=> 'setShutUpTime',
            'format'	=> 'json',
            'start'     => 0,
            'length'    => 200,
            'uid'       => $uid,
    );
    
     
    $content = get_api_content($_SC ['apiurl'], $param);

    $content = json_decode($content);
    $list = object_to_array($content->resultData);

    $mpurl  = 'admin.php?ac=manage&op=getShutUpList';
    $pageurl = multi($start, $length, $mpurl);
    $nextpurl  = $pageurl ['next'];
    $prevpurl  = $pageurl ['prev'];
    $mpurl     = $pageurl ['urls'];
    $firstpurl = $pageurl ['first'];
    
    $perpages = array(
            $length => ' selected'
    );
    include_once template("admin/".'manage');
    exit;
}elseif ($_GET ['op'] == "delUserCache"){
    $start              = intval($_GET ['start']) > 1 ? intval($_GET ['start']) : 0;
    $uid 	            = intval($_GET ['uid']);
    $length             = intval($_GET ['length']) > 1 ? intval($_GET ['length']) : 20;
    $param  = array(
            'do' 		=> 'delUserCache',
            'format'	=> 'json',
            'start'     => 0,
            'length'    => 5,
            'uid'       => $uid,
    );
    
     
    $content = get_api_content($_SC ['apiurl'], $param);

    $content = json_decode($content);
    $list = object_to_array($content->resultData);

    $mpurl  = 'admin.php?ac=manage&op=getShutUpList';
    $pageurl = multi($start, $length, $mpurl);
    $nextpurl  = $pageurl ['next'];
    $prevpurl  = $pageurl ['prev'];
    $mpurl     = $pageurl ['urls'];
    $firstpurl = $pageurl ['first'];
    
    $perpages = array(
            $length => ' selected'
    );
    include_once template("admin/".'manage');
    exit;
}elseif ($_GET ['op'] == "removeShutUp"){
    $uid    =    $_GET['uid'];
    $param = array(
            'do' => 'removeShutUp',
            'format' => 'json',
            'uid' => $uid,
    );
    $content = get_api_content($_SC ['apiurl'], $param);
    $content = json_decode($content);
    if ($content->resultData == 1) {

		delShutUpMsg($uid);

        $msg = "操作成功!";
        cpmessage($msg);
    }
    $msg = "操作失败，请重试一次!";
    if ($content->errorNo) {
        $msg .= "errorNo:" . $content->errorNo;
    }
    cpmessage($msg);
    exit;
}elseif($_GET ['op'] == "addAdmin"){
	$uid    =    $_GET['uid'];
	$param = array(
			'do' => 'addAdmin',
			'format' => 'json',
			'uid' => $uid,
	);

	$content = get_api_content($_SC ['apiurl'], $param);
	$content = json_decode($content);
	if ($content->resultData == 1) {
		$msg = "操作成功!";
		cpmessage($msg);
	}
	$msg = "操作失败，请重试一次!";
	if ($content->errorNo) {
		$msg .= "errorNo:" . $content->errorNo;
	}
	cpmessage($msg);
	exit;
}elseif ($_GET ['op'] == "fetchUserInfo") {

    // 列表
    $start = intval($_GET ['start']);
    // 参数过滤解析
    $perpage = intval($_GET ['perpage']) > 0 ? intval($_GET ['perpage']) : 20;

    // 搜索条件
    $intkeys = array(
        'uid'
    );
    $strkeys = array(
        'nickname',
        'account',
        'email',
        'deviceSN'
    );
    $results = getparam($intkeys, $strkeys);
    $mpurl .= '&' . implode('&', $results ['urls']);

    // 构建api请求url与参数数组
    $param = array(
        'do' => 'fetchUserInfo',
        'format' => 'json',
        'start' => $start,
        'length' => $perpage,
    );
    $param = array_merge($param, $results ['param']);
    // 读取api数据
    $content = get_api_content($_SC ['apiurl'], $param);
    $content = json_decode($content);
    if ($content->resultData) {
        $list = object_to_array($content->resultData);
    }

    // page
    $pageurl = multi($start, $perpage, $mpurl);
    $nextpurl = $pageurl ['next'];
    $prevpurl = $pageurl ['prev'];
    $mpurl = $pageurl ['urls'];
    $firstpurl = $pageurl ['first'];

    $perpages = array(
        $perpage => ' selected'
    );
}elseif ($_GET['op'] == 'hotdiary'){
    $param = array();
    if ($_GET['query'] == 1 ){
        $param	=  $_GET;
    }

    $param['uid']       =   $_SESSION['user']['adminUid'];
    $param['do']		=	'getHotDiary';
    $param['format']	=	'json';
    $start   			=   intval($_GET ['start']) > 0 ? intval($_GET ['start']) : 0;
    $perpage 			=   intval($_GET ['perpage']) > 0 ? intval($_GET ['perpage']) : 50;
    $param['start']		=	$start;
    $param['perpage']	=	$perpage;
    $param['length']	=	$perpage;
    $page    =    intval($_GET['page']) > 0 ? intval($_GET['page']) : 0 ;
    if ($page){
        $start             =    ($page-1)*$perpage;
        $param['start']	   =	$start;
    }
    $currtenPage        =   ceil($start/$perpage) + 1;
    $content  			=  	get_api_content($_SC ['apiurl'], $param);
    $result  			=  	json_decode($content);
    $result             =   object_to_array($result->resultData);

    $list               =   $result['status'];
    $totalNum           =   $result['counts'];
    $totalPages         =   ceil($totalNum/$perpage);
    
    // page
    $urls = '&query=1&op=hotdiary';
    $mpurl	   .= 	$urls;
    if ($page > 0){
        $mpurl	   .= 	$urls."&page=".$currtenPage;
    }
    $pageurl 	= 	multi($start, $perpage, $mpurl);
    $nextpurl 	= 	$pageurl['next'];
    $prevpurl 	= 	$pageurl['prev'];
    $mpurl 		= 	$pageurl['urls'];
    $firstpurl  = 	$pageurl['first'];
    $url = $_SERVER["QUERY_STRING"];
    $start = substr($url,48);
    
    include_once template("admin/manage_hotdiary");
    exit;
}elseif ($_GET['op'] == 'hotfilter'){
    $param = array();
    if ($_GET['query'] == 1 ){
        $param	=  $_GET;
    }

    $param['uid']       =   $_SESSION['user']['adminUid'];
    $param['do']		=	'getHotDiaryReady';
    $param['format']	=	'json';
    $start   			=   intval($_GET ['start']) > 0 ? intval($_GET ['start']) : 0;
    $perpage 			=   intval($_GET ['perpage']) > 0 ? intval($_GET ['perpage']) : 50;
    $param['start']		=	$start;
    $param['perpage']	=	$perpage;
    $param['length']	=	$perpage;
    $page    =    intval($_GET['page']) > 0 ? intval($_GET['page']) : 0 ;
    if ($page){
        $start             =    ($page-1)*$perpage;
        $param['start']	   =	$start;
    }
    $currtenPage        =   ceil($start/$perpage) + 1;
    $content  			=  	get_api_content($_SC ['apiurl'], $param);
    $result  			=  	json_decode($content);
    $result             =   object_to_array($result->resultData);
    
    $list               =   $result['status'];
    $totalNum           =   $result['counts'];
    $totalPages         =   ceil($totalNum/$perpage);
    
    // page
    $urls = '&query=1&op=hotfilter';
    $mpurl	   .= 	$urls;
    if ($page > 0){
        $mpurl	   .= 	$urls."&page=".$currtenPage;
    }
    $pageurl 	= 	multi($start, $perpage, $mpurl);
    $nextpurl 	= 	$pageurl['next'];
    $prevpurl 	= 	$pageurl['prev'];
    $mpurl 		= 	$pageurl['urls'];
    $firstpurl  = 	$pageurl['first'];
    $url = $_SERVER["QUERY_STRING"];
    $start = substr($url,48);
    include_once template("admin/manage_hotfilter");
    exit;
}elseif ($_GET['op'] == 'delHotDiaryReadyByBodyId'){
    $bodyId    =    $_GET['bodyId'];
    $param = array(
            'do'     => 'delHotDiaryReadyByBodyId',
            'format' => 'json',
            'bodyId' => $bodyId,
    );
    
    $content = get_api_content($_SC ['apiurl'], $param);
    $content = json_decode($content, true);
    $result = $content['resultData'];

    if ($result) {
        echo 1;
    }else{
        echo 0;
    }
    exit;
}elseif ($_GET['op'] == 'delHotDiaryByBodyId'){
    $bodyId    =    $_GET['bodyId'];
    $param = array(
            'do'     => 'delHotDiaryByBodyId',
            'format' => 'json',
            'bodyId' => $bodyId,
    );
    
    $content = get_api_content($_SC ['apiurl'], $param);
    $content = json_decode($content, true);
    $result = $content['resultData'];

    if ($result) {
        echo 1;
    }else{
        echo 0;
    }
    exit;
}elseif ($_GET['op'] == 'setHotDiaryData'){
    $bodyId    =    intval($_GET['bodyId']);
    $score     =    intval($_GET['score']);
    $param = array(
            'do'     => 'setHotDiaryData',
            'format' => 'json',
            'bodyId' => $bodyId,
            'score'  => $score,
    );

    $content = get_api_content($_SC ['apiurl'], $param);
    $content = json_decode($content, true);
    $result = $content['resultData'];

    $msg = '修改成功';
    if (!$result) {
        $msg = '修改失败';
    }

    echo "<script language='javascript' type='text/javascript'>window.history.go(-1);</script>";
    exit;
} elseif ($_GET['op'] == 'addHotDiaryData'){
    $bodyId    =    intval($_GET['bodyId']);
    $score     =    intval($_GET['score']);
    $param = array(
        'do'     => 'setHotDiaryData',
        'format' => 'json',
        'bodyId' => $bodyId,
        'score'  => $score,
    );
    $content = get_api_content($_SC ['apiurl'], $param);
    $content = json_decode($content, true);
    $result = $content['resultData'];

    $msg = '添加成功';
    if (!$result) {
        $msg = '添加失败';
    }

    echo "<script language='javascript' type='text/javascript'>window.history.go(-1);</script>";
    exit;
}elseif ($_GET['op'] == 'setHotDiaryCache'){
    $bodyId    =    intval($_GET['bodyId']);
    $score     =    intval($_GET['score']);
    $param = array(
        'do'     => 'setHotDiaryData',
        'format' => 'json',
        'bodyId' => $bodyId,
        'score'  => $score,
    );

    $content = get_api_content($_SC ['apiurl'], $param);
    $content = json_decode($content, true);
    $result = $content['resultData'];
    
    header('Content-type: application/json');
    echo json_encode($result);
    exit;
}else if ($_GET['op'] == 'setTopicHotDiaryCache'){
    $bodyId = intval($_GET['bodyId']);
    $topicId = intval($_GET['topicId']);
//    $score = intval($_GET['score']);
    $param = array(
        'do' => 'setTopicHotDiaryCache',
        'format' => 'json',
        'bodyId' => $bodyId,
        'topicId' => $topicId,
//        'score' => $score,
        'onlineTime' => 'all'
    );
    $content = get_api_content($_SC ['apiurl'], $param);
    $content = json_decode($content, true);
    $result = $content['resultData'];
    header('Content-type: application/json');
    echo json_encode($result);
    exit;
}else if ($_GET['op'] == 'delTopicHotDiaryCache'){
    $bodyId = intval($_GET['bodyId']);
    $topicId = intval($_GET['topicId']);
//    $score = intval($_GET['score']);
    $param = array(
        'do' => 'delTopicHotDiaryCache',
        'format' => 'json',
        'bodyId' => $bodyId,
        'topicId' => $topicId,
    );
    $content = get_api_content($_SC ['apiurl'], $param);
    $content = json_decode($content, true);
    $result = $content['resultData'];
    header('Content-type: application/json');
    echo json_encode($result);
    exit;
}else{
    $start              = intval($_GET ['start']) > 1 ? intval($_GET ['start']) : 0;
    $uid 	            = intval($_GET ['uid']);
    $length             = intval($_GET ['length']) > 1 ? intval($_GET ['length']) : 20;
    $length = 200;
    $param  = array(
            'do' 		=> 'getShutUpList',
            'format'	=> 'json',
            'start'     => $start,
            'length'    => $length,
            'uid'       => $uid,
    );
    
     
    $content = get_api_content($_SC ['apiurl'], $param);
    
    $content = json_decode($content);
    $list = object_to_array($content->resultData);
	$shutUpMsg = getShutUpMsg();
    $mpurl  = 'admin.php?ac=manage&op=getShutUpList';
    $pageurl = multi($start, $length, $mpurl);
    $nextpurl  = $pageurl ['next'];
    $prevpurl  = $pageurl ['prev'];
    $mpurl     = $pageurl ['urls'];
    $firstpurl = $pageurl ['first'];
    
    $perpages = array(
            $length => ' selected'
    );
    include_once template("admin/manage");
    exit;
}
function getShutUpMsg(){
	$file = '/tmp/forbiddenInfo/shutUpMsg.txt';
	$a = file_get_contents($file);
	$b = json_decode($a,true);
	return $b;
}
function delShutUpMsg($uid){
	$file = '/tmp/forbiddenInfo/shutUpMsg.txt';
	$b = array();
	$a = file_get_contents($file);
	if($a){
		$b = json_decode($a,true);
	}
	unset($b[$uid]);
	$c = json_encode($b);
	file_put_contents($file,$c);
}



